package server;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Map;

public class RequestProcessor extends Thread {

    private Socket socket;
    private Map<String, HttpServlet> servletMap;
    private Mapper mapper;

    public RequestProcessor(Socket socket, Mapper mapper) {
        this.socket = socket;
        this.mapper = mapper;
    }


    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();

            // 封装Request对象和Response对象
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());
            //从Mapper中获取Host->Context->Wrapper->Servelt
            Mapper.MappedHost mappedHost = null;
            for (int i = 0; i < mapper.hosts.length; i++) {
                if (mapper.hosts[i].name.equals(request.getIp())) {
                    mappedHost = mapper.hosts[i];
                }
            }
            if (mappedHost == null) {
                response.outputHtml(500, request.getIp());
            }

            Mapper.ContextList contextList = mappedHost.getContextList();
            Mapper.MappedContext mappedContext = null;
            for (int i = 0; i < contextList.contexts.length; i++) {
                if (request.getUrl()
                           .startsWith(contextList.contexts[i].name)) {
                    mappedContext = contextList.contexts[i];
                }
            }
            if (mappedContext == null) {
                response.outputHtml(404, request.getUrl());
            }
            Mapper.MappedWrapper mappedWrapper = null;
            for (int i = 0; i < mappedContext.wrappers.length; i++) {
                if (request.getUrl()
                           .endsWith(mappedContext.wrappers[i].name)) {
                    mappedWrapper = mappedContext.wrappers[i];
                }
            }
            if (mappedWrapper == null) {
                response.outputHtml(404, request.getUrl());
            }
            Wrapper wrapper = mappedWrapper.object;
            String servletClass = wrapper.getServletClass();
            //加载serlet
            String filepath = servletClass.replaceAll("\\.", "/");
            HttpServlet httpServlet = loadServlet(mappedHost.object.getAppBase() + "/" + mappedContext.object.getDocBase(), filepath);
            // 静态资源处理
//            if (servletMap.get(request.getUrl()) == null) {
//                response.outputHtml(request.getUrl());
//            } else {
//                // 动态资源servlet请求
//                HttpServlet httpServlet = servletMap.get(request.getUrl());
            httpServlet.service(request, response);
//            }

            socket.close();

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private HttpServlet loadServlet(String path, String filename) {
        DiskClassLoader diskClassLoader = new DiskClassLoader(path);
        try {
            Class<?> aClass = diskClassLoader.findClass(filename);
            HttpServlet httpServlet = (HttpServlet) aClass.newInstance();
            return httpServlet;
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
